﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CodeDemon.Models;

namespace CodeDemon.Controllers
{
	public class ReviewController : ControllerBase {
		[RequiresAuthentication]
		public ActionResult Index(string statusId) {
			ReviewModel model = new ReviewModel();
			if (statusId == null || statusId == String.Empty) {
				statusId = "1";
			}
			// Add action logic here
			PopulateModel( model );
			model.SelectedStatusIdForList = Int32.Parse( statusId );
			return View( "Index", model );
		}

		#region Review Display Run Modes
		[RequiresAuthentication]
		public ActionResult DisplayReview(int reviewId)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewID = reviewId;
			PopulateModel( model ); 
			return View( "DisplayReview", model );
			model.DAO.AddReviewLogEntry( reviewId, String.Format( "Attended Review" ), DateTime.Now, CurrentLoggedInReviewUserID, null );
			model.DAO.UpdateReviewParticipantVisit( reviewId, CurrentLoggedInReviewUserID );

		}

		public ActionResult DisplayReviewFile(int reviewFileId)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewFileID = reviewFileId;
			PopulateModel(model);
			var thisReview = (from reviews in model.DAO.GetReviewByReviewFileID(reviewFileId) select reviews).FirstOrDefault();
			if (model.CurrentReviewUserID != 0)
			{
				model.InsertReviewLogEntry(thisReview.ReviewID.Value, String.Format("Reviewed File"), reviewFileId);
			}
			return View("DisplayReviewFile", model);
		}

		public ActionResult DisplayReviewFileSingle(int reviewFileId)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewFileID = reviewFileId;
			PopulateModel(model);
			return View("DisplayReviewFileSingle", model);
		}

		public ActionResult DisplayFileComments(int reviewFileId)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewFileID = reviewFileId;
			return View("DisplayReviewFileComments", model);
		}

		public ActionResult DisplayFileLineComments(int reviewFileId, int lineNumber)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewFileID = reviewFileId;
			model.CurrentLineNumber = lineNumber;
			return View("DisplayFileLineComments", model);
		}

		[RequiresAuthentication(AjaxControllerAction = true)]
		public ActionResult DisplayReviewLog(int reviewId)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewID = reviewId;
			PopulateModel(model);
			return View("DisplayReviewLog", model);
		}

		public ActionResult DisplayReviewComments(int reviewId)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewID = reviewId;
			PopulateModel(model);
			return View("DisplayReviewComments", model);
		}

		#endregion Review Display Run Modes

		#region Review Edit & Update Run Modes
		[RequiresAuthentication(AjaxControllerAction = true)]
		public ActionResult EditReview(int reviewId)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewID = reviewId;
			PopulateModel( model ); 
			var review = (from reviews in model.DAO.GetReviewsById( reviewId ) select reviews).FirstOrDefault();
			if (review.ReviewOwnerID == CurrentLoggedInReviewUserID) {
				return View( "EditReview", model );
			} else {
				GenericActionModel genModel = new GenericActionModel();
				genModel.ErrorMessage = "You are not authorized to edit this review";
				return View( "Error", genModel );
			}
		}

		[RequiresAuthentication]
		public ActionResult UpdateReviewDetails(int reviewId, string title, string description)
		{
			ReviewModel model = new ReviewModel();
			var review = (from reviews in model.DAO.GetReviewsById( reviewId ) select reviews).FirstOrDefault();
			if (review.ReviewOwnerID == CurrentLoggedInReviewUserID) {
				model.UpdateReviewDetails( reviewId, title, description );
				return View( "Success" );
			} else {
				GenericActionModel genModel = new GenericActionModel();
				genModel.ErrorMessage = "You are not authorized to edit this review";
				return View( "Error", genModel );
			}
		}

		[RequiresAuthentication(AjaxControllerAction = true)]
		public ActionResult EditApprovalStatus(int reviewId)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewID = reviewId;
			PopulateModel(model);
			return View("EditApprovalStatus", model);
		}

		[RequiresAuthentication]
		public ActionResult UpdateApprovalStatus(int reviewId, int newStatusId)
		{
			ReviewModel model = new ReviewModel();
			PopulateModel(model);
			model.UpdateApprovalStatus(reviewId, newStatusId);
			model.SelectedStatusIdForList = 1;
			model.CurrentReviewID = reviewId;
			return View("DisplayReview", model);
		}

		[RequiresAuthentication(AjaxControllerAction = true)]
		public ActionResult JoinReview(int reviewId, bool asReviewer)
		{
			ReviewModel model = new ReviewModel();
			PopulateModel(model);
			model.InsertReviewParticipant(reviewId, CurrentLoggedInReviewUserID, asReviewer);
			return View("Success");
		}
		#endregion Review Edit & Update Run Modes

		#region Review Comments Run Modes
		[RequiresAuthentication(AjaxControllerAction = true)]
		public ActionResult NewReviewComment(int reviewId)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewID = reviewId;
			PopulateModel( model );
			return View( "NewReviewComment", model );
		}

		[RequiresAuthentication(AjaxControllerAction = true)]
		public ActionResult InsertReviewComment(int reviewId, string commentText, short priority, int severityID, string change_status, string newStatusId)
		{
			ReviewModel model = new ReviewModel();
			PopulateModel( model );
			model.InsertReviewComment( reviewId, commentText, CurrentLoggedInReviewUserID ,priority, severityID);
			if (change_status == "yes")
			{
				model.UpdateApprovalStatus(reviewId, Int32.Parse(newStatusId));
			}

			return View( "Success" );
		}

		[RequiresAuthentication(AjaxControllerAction = true)]
		public ActionResult NewReviewFileComment(int reviewFileId, int lineNumber)
		{
			ReviewModel model = new ReviewModel();
			model.CurrentReviewFileID = reviewFileId;
			PopulateModel( model ); 
			model.CurrentLineNumber = lineNumber;
			return View( "NewReviewFileComment", model );
		}

		[RequiresAuthentication(AjaxControllerAction = true)]
		public ActionResult InsertReviewFileComment(int reviewFileId, string commentText, int lineNumber, short priority, int severityID, string change_status, string newStatusId)
		{
			ReviewModel model = new ReviewModel();
			PopulateModel( model );
			model.InsertReviewFileComment(reviewFileId, commentText, lineNumber, CurrentLoggedInReviewUserID, priority, severityID);
			var review = (from reviews in model.DAO.GetReviewByReviewFileID(reviewFileId) select reviews).FirstOrDefault();
			if (change_status == "yes")
			{
				model.UpdateApprovalStatus(review.ReviewID.Value, Int32.Parse(newStatusId));
			}
			return View( "Success" );
		}

		#endregion Review Comments Run Modes

		#region Review Search Run Modes
		[RequiresAuthentication]
		public ActionResult DisplaySearchForm()
		{
			ReviewModel model = new ReviewModel();
			PopulateModel(model);
			return View("DisplaySearchForm", model);
		}

		[RequiresAuthentication]
		public ActionResult DisplaySearchResults(string reviewTitle, string reviewDesc, string reviewStatusID, string reviewOwnerID)
		{
			ReviewModel model = new ReviewModel();
			PopulateModel(model);

			if (reviewTitle == null || reviewTitle == string.Empty)
			{
				model.VarHash.Add("reviewTitle", "%");
			}
			else
			{
				model.VarHash.Add("reviewTitle", "%" + reviewTitle + "%");
			}

			if (reviewDesc == null || reviewDesc == string.Empty)
			{
				model.VarHash.Add("reviewDesc", "%");
			}
			else
			{
				model.VarHash.Add("reviewDesc", "%" + reviewDesc + "%");
			}

			if (reviewStatusID == null || reviewStatusID == string.Empty)
			{
				model.VarHash.Add("filterByStatus", "no");
			}
			else
			{
				model.VarHash.Add("filterByStatus", "yes");
				model.VarHash.Add("reviewStatusID", reviewStatusID);
			}

			if (reviewOwnerID == null|| reviewOwnerID == string.Empty)
			{
				model.VarHash.Add("filterByOwner", "no");
			}
			else
			{
				model.VarHash.Add("filterByOwner", "yes");
				model.VarHash.Add("reviewOwnerID", reviewOwnerID);
			}

			return View("DisplaySearchResults", model);

		}

		#endregion Review Search Run Modes

		#region Review & Defect Report Run Modes
		[RequiresAuthentication]
		public ActionResult DisplayDefectReport(string defectID)
		{
			if (defectID == null)
			{
				GenericActionModel genModel = new GenericActionModel();
				genModel.ErrorMessage = "No defect ID supplied.";
				return View("Error", genModel);
			}
			else
			{
				ReviewModel model = new ReviewModel();
				PopulateModel(model);
				model.VarHash.Add("defectID", defectID);
				return View("DisplayDefectReport", model);
			}

		}


		#endregion Review & Defect Report Run Modes

		[RequiresAuthentication(AjaxControllerAction=true)]
		public ActionResult CurrentReviewMenu()
		{
			ReviewModel model = new ReviewModel();
			PopulateModel( model );
			return View( "CurrentReviewMenu", model );
		}
	}
}

